//
//  TTDianpuLuoshiViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/11/11.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDianpuLuoshiViewCell.h"
#import "TTUpDownButton.h"
#import "TTOrderShopModel.h"
#import <UIImageView+WebCache.h>
#import "TTCommondDataModel.h"
#import "YBImageBrowser.h"

@interface TTDianpuLuoshiViewCell ()

@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UIImageView *sellOutImageView;
@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UILabel *oneLabel;
@property (nonatomic, strong) UILabel *twoLabel;
@property (nonatomic, strong) UILabel *threeLabel;
@property (nonatomic, strong) UIButton *fourBtn;
@property (nonatomic, strong) TTUpDownButton *locationBtn;
@property (nonatomic, strong) UILabel *priceLabel;  // 价格
@property (nonatomic, strong) UIView *lineView;

@end

@implementation TTDianpuLuoshiViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = UIColorHex(F1F5F6);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.subject = [RACSubject subject];
        [self setupSubviews];
        
    }
    return self;
}

- (void)setModel:(TTOrderShopModel *)model
{
    _model = model;
    
    self.leftBtn.selected = model.isSelected;
    self.leftBtn.enabled = model.SysStatus.intValue == 1;
    
    NSString *imageUrlString = [model.GoodsImage urlEncode];
    [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:imageUrlString] placeholderImage:[UIImage imageNamed:@"f_placeholder_img"] options:SDWebImageRetryFailed];
    self.leftLabel.text = model.ProductText;
    
    self.oneLabel.text = model.GoodsTitle;
    NSData *data = [model.GoodsDescribeTaotuo dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *describe = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    self.twoLabel.text = [describe componentsJoinedByString:@"\n"];
    
    NSString *value = @"";
    if (model.ClarityCharacteristics.length > 1) {
        NSDictionary *dic = [TTCommondDataModel getClarityCharacteristics];
        NSArray *characteristicsArray = [model.ClarityCharacteristics componentsSeparatedByString:@","];
        for (NSString *str in characteristicsArray) {
            NSString *key = [str removeFirstAndLastBlank];
            if (dic[key]) {
                value = [NSString stringWithFormat:@"%@%@ ", value, dic[key]];
            }
        }
    }
    if (value.length == 0) {
        self.threeLabel.text = model.ClarityCharacteristics;
    } else {
        self.threeLabel.text = value;
    }
    
    if (model.SouthAfrica) {
        [self.fourBtn setTitle:model.SouthAfricaText forState:UIControlStateNormal];
        self.fourBtn.enabled = NO;
    } else {
        [self.fourBtn setTitleColor:UIColorHex(333333) forState:UIControlStateNormal];
        if (model.GoodsCertPic.length > 0) {
            NSString *zsNum = [NSString stringWithFormat:@"%@ %@", model.GoodsCertType, model.GoodsCertNo];
            NSString *zsStr = [NSString stringWithFormat:@"%@ 证书预览", zsNum];
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:zsStr];
            [attr addAttributes:@{NSUnderlineStyleAttributeName : @1, NSUnderlineColorAttributeName : kMainColor, NSForegroundColorAttributeName : kMainColor} range:[zsStr rangeOfString:@"证书预览"]];
            [attr addAttribute:NSForegroundColorAttributeName value:UIColorHex(333333) range:[zsStr rangeOfString:zsNum]];
            
            [self.fourBtn setAttributedTitle:attr forState:UIControlStateNormal];
            self.fourBtn.enabled = YES;
        } else {
            NSString *zsStr = [NSString stringWithFormat:@"%@ %@", model.GoodsCertType, model.GoodsCertNo];
            [self.fourBtn setTitle:zsStr forState:UIControlStateNormal];
            self.fourBtn.enabled = NO;
        }
    }
    
    [self.locationBtn setTitle:model.GoodsLocation forState:UIControlStateNormal];
    self.locationBtn.hidden = model.GoodsLocation.length == 0;
    
    self.priceLabel.text = [NSString stringWithFormat:@"￥%@", model.PlusShopGoodsPrice];
    
    
    NSString *sellImageName = @"";
    if (model.SysStatus.intValue == 2) {
        sellImageName = @"shopcartsellout";
    } else if (model.SysStatus.intValue == 3) {
        sellImageName = @"xiajia";
    }
    self.sellOutImageView.image = [UIImage imageNamed:sellImageName];
    self.sellOutImageView.hidden = model.SysStatus.intValue == 1;
    
    
}


// 查看证书
- (void)lookZSBtnClick
{
    NSString *urlString = self.model.GoodsCertPic;
    [TTLoginSingle lookCertificateWithUrlString:urlString];
}

- (void)leftBtnClick:(UIButton *)sender
{
    sender.selected = !sender.selected;
    self.model.isSelected = sender.selected;
    [self.subject sendNext:self.model];
}

- (void)setupSubviews
{
    _leftBtn = [UIButton createButtonWithNormalImage:[UIImage imageNamed:@"shopcart_unsel"] selectedImage:[UIImage imageNamed:@"shopcartlist_sel"] target:self action:@selector(leftBtnClick:)];
    [_leftBtn setImage:[UIImage imageNamed:@"shopcart_unable"] forState:UIControlStateDisabled];
    _leftBtn.imageView.contentMode = UIViewContentModeCenter;
    [self.contentView addSubview:_leftBtn];
    [_leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self.contentView);
        make.width.mas_equalTo(30 * kScale);
    }];
    
    _leftImageView = [[UIImageView alloc] init];
    _leftImageView.layer.cornerRadius = 10;
    _leftImageView.layer.masksToBounds = YES;
    _leftImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:_leftImageView];
    [_leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftBtn.mas_right);
        make.centerY.equalTo(self.contentView).mas_offset(-12);
        make.width.height.mas_equalTo(65 * kScale);
    }];
    
    _sellOutImageView = [[UIImageView alloc] init];
    _sellOutImageView.contentMode = UIViewContentModeCenter;
    [self.contentView addSubview:_sellOutImageView];
    [_sellOutImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.leftImageView);
    }];
    
    _leftLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:9];
    [self.contentView addSubview:_leftLabel];
    [_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leftImageView.mas_bottom).mas_offset(5);
        make.centerX.equalTo(self.leftImageView);
    }];
    
    _oneLabel = [UILabel labelWithTitle:@"" color:[UIColor blackColor] fontSize:15];
    _twoLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:12];
    _threeLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:8];
    _fourBtn = [UIButton createButtonWithTitle:@"证书预览" titleColor:UIColorHex(333333) titleFont:[UIFont systemFontOfSize:12] backgroundColor:[UIColor clearColor] target:self action:@selector(lookZSBtnClick)];
    
    UIStackView *stView = [[UIStackView alloc] init];
    stView.axis = UILayoutConstraintAxisVertical;
    stView.distribution = UIStackViewDistributionFill;
    stView.alignment = UIStackViewAlignmentLeading;
    stView.spacing = 5;
    [self.contentView addSubview:stView];
    [stView addArrangedSubview:_oneLabel];
    [stView addArrangedSubview:_twoLabel];
    [stView addArrangedSubview:_threeLabel];
    [stView addArrangedSubview:_fourBtn];
    [stView addArrangedSubview:[UIView new]];
    [stView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.equalTo(self.leftImageView.mas_right).mas_offset(10);
        make.bottom.equalTo(self.contentView);
        make.right.equalTo(self.contentView).mas_offset(-10);
    }];
    
     _locationBtn = [TTUpDownButton buttonWithType:UIButtonTypeCustom];
        [_locationBtn setTitleColor:UIColorHex(DEDEDE) forState:UIControlStateNormal];
        [_locationBtn setImage:[UIImage imageNamed:@"shopcart_dizhi"] forState:UIControlStateNormal];
    _locationBtn.titleLabel.font = [UIFont systemFontOfSize:9];
//    _locationBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [self.contentView addSubview:_locationBtn];
        [_locationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(stView);
            make.right.equalTo(self.contentView);
            make.width.mas_equalTo(40);
            make.height.mas_equalTo(30);
        }];
    
    // 价格
    _priceLabel = [UILabel labelWithTitle:@"" color:UIColorHex(FF1F1F) fontSize:15];
    [self.contentView addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView).mas_offset(-10);
        make.right.equalTo(self.contentView).mas_offset(-10);
    }];
    
    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_lineView];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.equalTo(self.contentView).mas_offset(-10);
        make.bottom.equalTo(self.contentView).mas_offset(1);
        make.height.mas_equalTo(1);
    }];
    
}



@end
